
// 数字千分位加逗号输出
const toThousands = (num) => {
  let number = num.toString().indexOf('.') !== -1 ? num.toString().split('.')[0] : (num || 0).toString()
  let result = num.toString().indexOf('.') !== -1 ? ('.' + num.toString().split('.')[1]) : ''
  while (number.length > 3) {
    result = ',' + number.slice(-3) + result
    number = number.slice(0, number.length - 3)
  }
  if (number) {
    result = number + result
  }
  return result
}

const format = {
  install: (Vue) => {
    Vue.prototype.$formatNumber = (dataArr) => {
      let newData = dataArr.map((item) => {
        if (parseInt(item.content) > 999) {
          item.content = toThousands(item.content)
        }
        return item
      })
      return newData
    }
  }
}

export default format
